package com.fg.cloud.framework.logistics;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.kuaidi.HttpUtils;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.framework.controller.BaseController;

/**
 * Description:阿里云市场 -- 物流接口
 * @author around
 * @date 2017年11月9日下午3:11:26
 */
@Controller
@RequestMapping("/alilogistics")
public class AliLogisticsController extends BaseController {
	
	private static final String APPCODE = "77fbb0efaece4070a693d337f40640ec";
	
	private static final String APPSECRET = "d5b876baeef542e96f6a93b2fa6d45a9";
	
	private static final String APPID = "24687361";
	
	private static final String HOST = "http://jisukdcx.market.alicloudapi.com";
	
	private static final String PATH = "/express/query";
	
	
	
	/**
	 * Description:基础物流信息查询
	 * @param response
	 * @param type	物流公司编码
	 * @param number 物流单号
	 * @author around
	 * @date 2017年11月10日上午9:01:34
	 */
	@RequestMapping(value = "/base")
	public void base(HttpServletResponse response, String type, String number) {
		if (StringUtils.isEmpty(number)) {
			doResult(response, false, StatusCode.DEFAULT, null, "未找到对应物流信息");
			return;
		}
		if (StringUtils.isEmpty(type)) {
			auto(response, number);
			return;
		}
		
		Map<String, String> headers = new HashMap<String, String>();
	    headers.put("Authorization", "APPCODE " + APPCODE);
	    Map<String, String> querys = new HashMap<String, String>();
	    querys.put("number", number);
	    querys.put("type", type);

		try {
			HttpResponse hr = HttpUtils.doGet(HOST, PATH, "GET", headers, querys);
			//System.out.println(hr.toString());
			//System.out.println(EntityUtils.toString(hr.getEntity()));
			JSONObject json = JSONObject.parseObject(EntityUtils.toString(hr.getEntity()));
			doResult(response, true, StatusCode.DEFAULT,json.get("result"), ResultType.FIND_SUCCESS);
			
		} catch (Exception e) {
			doResult(response, false, StatusCode.DEFAULT, null, "未找到对应物流信息");
			e.printStackTrace();
		}
	}
	
	
	/**
	 * Description:自动查询物流信息
	 * @param response
	 * @param number 物流单号
	 * @author around
	 * @date 2017年11月10日上午9:02:05
	 */
	@RequestMapping(value = "/auto")
	public void auto(HttpServletResponse response, String number) {
		Map<String, String> headers = new HashMap<String, String>();
	    //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
	    headers.put("Authorization", "APPCODE " + APPCODE);
	    Map<String, String> querys = new HashMap<String, String>();
	    querys.put("number", number);
	    querys.put("type", "AUTO");
	    
		try {
			HttpResponse hr = HttpUtils.doGet(HOST, PATH, "GET", headers, querys);
			JSONObject json = JSONObject.parseObject(EntityUtils.toString(hr.getEntity()));
			doResult(response, true, StatusCode.DEFAULT,json.get("result"), ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			doResult(response, false, StatusCode.DEFAULT, null, "未找到对应物流信息");
			e.printStackTrace();
		}
	}
	
	
}
